package com.zztl.infiniteBand.ui.view.dialog

import android.app.Dialog
import android.content.Context
import android.support.annotation.StyleRes
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView

import com.zztl.infiniteBand.R


/**
 * @desc
 * @auth ${user}
 * @time 2018/1/9  15:12
 */

class ExitDialog : Dialog {
    class Builder(private val context: Context) {
        private var message: String? = null
        private var isShowMessage = true
        private var isCancelable = true
        private var isCancelOutside = true
        private var affirmListener: ClickListener? = null
        private var cancelListener: ClickListener? = null
        private var affirmTxt: String? = null
        private var cancelTxt: String? = null
        private var btnColor = -1

        /**
         * 设置提示信息
         *
         * @param message
         * @return
         */

        fun setMessage(message: String): Builder {
            this.message = message
            return this
        }

        /**
         * 设置是否显示提示信息
         *
         * @param isShowMessage
         * @return
         */
        fun setShowMessage(isShowMessage: Boolean): Builder {
            this.isShowMessage = isShowMessage
            return this
        }

        /**
         * 设置是否可以按返回键取消
         *
         * @param isCancelable
         * @return
         */

        fun setCancelable(isCancelable: Boolean): Builder {
            this.isCancelable = isCancelable
            return this
        }

        /**
         * 设置是否可以取消
         *
         * @param isCancelOutside
         * @return
         */
        fun setCancelOutside(isCancelOutside: Boolean): Builder {
            this.isCancelOutside = isCancelOutside
            return this
        }

        fun setClickListener(listener: ClickListener): Builder {
            this.affirmListener = listener
            this.cancelListener = listener
            return this
        }

        /*  public Builder setCancelListener(ClickListener cancelListener) {
              this.cancelListener = cancelListener;
              return this;
          }
  */
        fun setAffirmTxt(affirmTxt: String): Builder {
            this.affirmTxt = affirmTxt
            return this
        }

        fun setCancelTxt(cancelTxt: String): Builder {
            this.cancelTxt = cancelTxt
            return this
        }

        fun setBtnColor(color: Int): Builder {
            this.btnColor = color
            return this
        }


        fun create(): ExitDialog {

            val inflater = LayoutInflater.from(context)
            val view = inflater.inflate(R.layout.layout_dialog_exit, null)
            val dialog = ExitDialog(context, R.style.CommomDialog)

            val msgText = view.findViewById<View>(R.id.title) as TextView
            val submitBtn = view.findViewById<View>(R.id.submit) as TextView
            val cancelBtn = view.findViewById<View>(R.id.cancel) as TextView
            if (null != affirmTxt) {
                submitBtn.text = affirmTxt
            }
            if (null != cancelTxt) {
                cancelBtn.text = cancelTxt
            }

            if (-1 != btnColor) {
                submitBtn.setTextColor(context.resources.getColor(btnColor))
                cancelBtn.setTextColor(context.resources.getColor(btnColor))
            }

            submitBtn.setOnClickListener {
                dialog.dismiss()
                if (null != affirmListener) {
                    affirmListener!!.comfirm()
                }
            }


            cancelBtn.setOnClickListener {
                dialog.dismiss()
                if (null != cancelListener) {
                    cancelListener!!.cancel()
                }
            }

            if (isShowMessage) {
                msgText.text = message
            } else {
                msgText.visibility = View.GONE
            }
            dialog.setContentView(view)
            dialog.setCancelable(isCancelable)
            dialog.setCanceledOnTouchOutside(isCancelOutside)
            return dialog

        }


        interface ClickListener {
            fun cancel()

            fun comfirm()
        }


    }



    constructor(context: Context, @StyleRes themeResId: Int) : super(context, themeResId)

}
